package com.jinshan.gaietymall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinshan.gaietymall.core.exception.BusinessException;
import com.jinshan.gaietymall.entity.TRole;
import com.jinshan.gaietymall.entity.request.role.TRoleIdReqVO;
import com.jinshan.gaietymall.entity.request.role.TRoleInsertReqVO;
import com.jinshan.gaietymall.entity.request.role.TRoleQueryVO;
import com.jinshan.gaietymall.entity.response.role.TRoleRep;
import com.jinshan.gaietymall.exception.ErrorCodesEnum;
import com.jinshan.gaietymall.mapper.TRoleMapper;
import com.jinshan.gaietymall.service.ITRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author liujinshan
 * @since 2020-04-15
 */
@Service
public class TRoleServiceImpl extends ServiceImpl<TRoleMapper, TRole> implements ITRoleService {

    @Autowired
    private TRoleMapper tRoleMapper;

    /**
     * @description 分页查询
     * @author liujinshan
     * @date 2020/4/24 15:25
     */
    @Override
    public IPage<TRoleRep> getPage(TRoleQueryVO tRoleQueryVO) {

        QueryWrapper<TRole> queryWrapper = new QueryWrapper<>();

        IPage<TRoleRep> roleRepPage = new Page<>();

        IPage<TRole> page = new Page<>();

        String roleName = Optional.ofNullable(tRoleQueryVO).map(TRoleQueryVO::getRoleName).orElse(null);

        Integer currentPage = Optional.ofNullable(tRoleQueryVO).map(TRoleQueryVO::getCurrentPage).orElse(null);
        Integer pageSize = Optional.ofNullable(tRoleQueryVO).map(TRoleQueryVO::getPageSize).orElse(null);

        if (null != currentPage && null != pageSize) {
            page.setCurrent(tRoleQueryVO.getCurrentPage());
            page.setSize(tRoleQueryVO.getPageSize());
        }

        if (null != roleName && !"".equals(roleName)) {
            queryWrapper.like("roleName",tRoleQueryVO.getRoleName());
        }
        try {

            page = tRoleMapper.selectPage(page,queryWrapper);

            BeanUtils.copyProperties(page,roleRepPage);

        } catch (BusinessException e) {
            throw new BusinessException(ErrorCodesEnum.NONE_QUERY_DATA.code(),ErrorCodesEnum.NONE_QUERY_DATA.msg());
        } catch (Exception e) {
            throw new BusinessException(ErrorCodesEnum.OTHER.code(),ErrorCodesEnum.OTHER.msg());
        }
        return roleRepPage;
    }

    /**
     * @param tRoleIdReqVO
     * @description 根据id查询详情
     * @author liujinshan
     * param id
     * @date 2020/4/27 18:56
     */
    @Override
    public TRoleRep getById(TRoleIdReqVO tRoleIdReqVO) {

        Long id = Optional.ofNullable(tRoleIdReqVO).map(TRoleIdReqVO::getId).orElse(null);

        if(null == id ) {

        }
        TRoleRep tRoleRep = new TRoleRep();

        TRole tRole = new TRole();
        tRole = tRoleMapper.selectById(tRoleIdReqVO.getId());

        BeanUtils.copyProperties(tRole,tRoleRep);

        return tRoleRep;
    }

    /**
     * @param tRoleInsertReqVO
     * @description 新增
     * @author liujinshan
     * param tRoleInsertReqVO
     * @date 2020/4/29 13:46
     */
    @Override
    public void add(TRoleInsertReqVO tRoleInsertReqVO) {
        TRole tRole = new TRole();
        BeanUtils.copyProperties(tRoleInsertReqVO,tRole);
        int i = 0;
        i = tRoleMapper.insert(tRole);
        if (i<=0) {
            throw new BusinessException(ErrorCodesEnum.OTHER.code(),ErrorCodesEnum.OTHER.msg());
        }

    }
}
